package com.autonavi.minimap.net;

import java.net.InetSocketAddress;

import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.params.ConnRouteParams;

import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Proxy;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.text.TextUtils;

public class ProxyUtil {

	/**
	 * Set APN for HTTP Client. Use old way to get host and port.
	 * 
	 * @param httpClient
	 * @param context
	 */
	public static void setProxyHttpHost(HttpClient httpClient, Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo != null
				&& networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
			// GPRS: APN http proxy
			final String host = Proxy.getDefaultHost();
			if (!TextUtils.isEmpty(host)) {
				HttpHost proxyhost = new HttpHost(host, Proxy.getDefaultPort());
				httpClient.getParams().setParameter(
						ConnRoutePNames.DEFAULT_PROXY, proxyhost);
			}
		}
	}

	public static java.net.Proxy getProxy(Context context) {
		java.net.Proxy result = null;
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo != null
				&& networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
			// GPRS: APN http proxy
			final String host = Proxy.getDefaultHost();
			if (!TextUtils.isEmpty(host)) {
				result = new java.net.Proxy(java.net.Proxy.Type.HTTP,
						new InetSocketAddress(host, Proxy.getDefaultPort()));
			}
		}
		return result;

	}

}